﻿namespace BEGDC.CC.Web.Common
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using BEGDC.CC.BL.Common;
    using BEGDC.CC.BL.Common.Query;
    using BEGDC.CC.BL.Workflow;
    using BEGDC.Utility.Castle;
    using BEGDC.CC.BL.UserManagement;
    using BEGDC.CC.Entity;

    /// <summary>
    ///	Summary description for StampCtrl.
    /// </summary>
    public class StampCtrl2 : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.Label lblRunFlowRoleName;
        protected System.Web.UI.WebControls.Label lblRunFlowActorsName;
        protected System.Web.UI.WebControls.Label lblRunFlowRoleNameStr;
        protected System.Web.UI.WebControls.Label lblRunFlowActorsNameStr;

        #region ActionName

        private const string Submit = "Submit";
        private const string AskForMore = "AskForMore";
        private const string ReplyAskForMore = "ReplyAskForMore";
        private const string Query = "Query";
        private const string ReplyQuery = "ReplyQuery";
        private const string Reject = "Reject";
        private const string Approve = "Approve";
        private const string Assign = "Assign";
        private const string Check = "Check";
        private const string Close = "Close";
        private const string CheckMM = "CheckMM";

        #endregion

        #region Step

        private const string End = "End";
        private const string StepClose = "Close";
        private const string CloseApp = "CloseApp";
        private const string Pending = "Pending";
        private const string ReRequest = "ReRequest";

        #endregion

        #region CRStep

        private const string CRStepCodeCATVPApproval = "CATVPApproval";
        private const string CRStepCodeFNCApproval = "FNCApproval";
        private const string CRStepCodeDeptHeadApproval = "DeptHeadApproval";
        private const string CRStepCodeBDDirectorApproval = "BDDirectorApproval";
        private const string CRStepCodeBDHeadApproval = "BDHeadApproval";
        private const string CRStepCodeCRSurvey = "CRSurvey";
        private const string CRStepDeptDirectorApproval = "DeptDirectorApproval";
        private const string CRStepCloseApp = "CloseApp";

        #endregion

        #region  EVStep

        private const string EVStepCodeClose = "Close";
        private const string EVStepStaffCheckMM = "StaffCheckMM";
        private const string EVStepCATApproveMM = "CATApproveMM";
        private const string EVStepPSOApproveMM = "PSOApproveMM";
        private const string EVStepMeetingMinutes = "MeetingMinutes";

        #endregion

        //int AppType 
        private const int CR = 1;
        private const int EV = 3;
        private const int OCR = 390;
        private const int OCV = 391;

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (this.Request.QueryString["AppID"] != null)
            {
                int appid = Convert.ToInt32(this.Request.QueryString["AppID"]);
                this.GetStampByAppID(appid);
            }
            else
            {
                this.SetDataSource(new DataTable());
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region GetAppType

        private int GetAppTypeIDByPDType()
        {
            string appTypeStr = this.Request.QueryString["PDType"];
            IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            return queryService.GetAppTypeID(appTypeStr);
        }

        #endregion

        #region Pendding StampCellList

        private ArrayList StampCellList(ArrayList cellArray, int targetStep, bool isOnlyShowNextOneStep)
        {
            IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;

            // Get AppType ID
            int appTypeID = GetAppTypeIDByPDType();

            // Get StepID in table StepOrder
            string stepOrderID = queryService.GetStepFromStepOrder(targetStep, appTypeID);
            if (string.IsNullOrEmpty(stepOrderID)) return cellArray;

            //Get Steps form StepOrder
            DataTable table = queryService.QueryActionNextSteps(int.Parse(stepOrderID), appTypeID);
            if (table != null && table.Rows.Count > 0)
            {
                DataRow[] rows = table.Select();

                for (int i = 0; i < rows.Length; i++)
                {
                    string stepCode = rows[i]["StepCode"].ToString();

                    //Step Description
                    string description = rows[i]["Description"].ToString();
                    description = description.Contains("SSMEHeadApproval") ? description.Replace("SSMEHeadApproval", "IBGHeadApproval") : description;

                    //Don't show steps after End
                    if (stepCode == End) break;

                    //Only show one next step
                    if (isOnlyShowNextOneStep && i > 0) break;

                    //CR:unShow CATVPApproval FNCApproval DeptHeadApproval BDDirectorApproval BDHeadApproval CRSurvey
                    //if (appTypeID == CR && (stepCode == CRStepCodeCATVPApproval ||
                    //    stepCode == CRStepCodeFNCApproval || stepCode == CRStepCodeDeptHeadApproval ||
                    //    stepCode == CRStepCodeBDDirectorApproval || stepCode == CRStepCodeBDHeadApproval
                    //    || stepCode == CRStepCodeCRSurvey))
                    //    continue;

                    //EV:unShow ApproveMM StaffCheckMM
                    //if (appTypeID == EV && (stepCode == BEGDC.CC.BL.Workflow.WorkTransition.EV_CATApproveMM
                    //    || stepCode == BEGDC.CC.BL.Workflow.WorkTransition.EV_PSOApproveMM ||
                    //    stepCode == BEGDC.CC.BL.Workflow.WorkTransition.EV_CODStaffCheckMM))
                    //    continue;

                    //OCR:unshow DeptHeadApproval FNCApproval
                    //if (appTypeID == OCR && (stepCode == BEGDC.CC.BL.Workflow.WorkTransition.OCR_DeptHeadApproval
                    //    || stepCode == BEGDC.CC.BL.Workflow.WorkTransition.OCR_FNCApproval))
                    //    continue;

                    //OCV:unshow DepartmentVPApproval DepartmentFNCApproval OCVSurvey
                    //if (appTypeID == OCV && (stepCode == BEGDC.CC.BL.Workflow.WorkTransition.OCV_DepartmentVPApproval
                    //    || stepCode == BEGDC.CC.BL.Workflow.WorkTransition.OCV_DepartmentFNCApproval ||
                    //    stepCode == BEGDC.CC.BL.Workflow.WorkTransition.OCV_OCVSurvey))
                    //    continue;

                    //If first step is close, don't show next steps
                    if (i == 0 && (stepCode == StepClose || stepCode == CloseApp))
                    {
                        this.lblRunFlowRoleNameStr.Visible = true;
                        this.lblRunFlowRoleName.Text = description;
                        this.SetLblRunFlowActorsName(description, stepCode, appTypeID);
                        cellArray.Add(this.GetStampCell(stepCode, DateTime.Now.ToString("MM/dd/yyyy"), Pending, "PendingY.gif", Color.Yellow));
                        break;
                    }

                    //Step equals request
                    if (i == 0 && stepCode == ReRequest)
                    {
                        this.lblRunFlowRoleNameStr.Visible = true;
                        this.lblRunFlowRoleName.Text = description;
                        this.SetLblRunFlowActorsName(description, stepCode, appTypeID);
                        cellArray.Add(this.GetStampCell(stepCode, DateTime.Now.ToString("MM/dd/yyyy"), Pending, "PendingY.gif", Color.Yellow));
                        break;
                    }

                    //Is show ReportToApprove
                    if (i == 0 && this.IsReportToApprove(stepCode, appTypeID, cellArray))
                    {
                        this.lblRunFlowRoleNameStr.Visible = true;
                        this.lblRunFlowRoleName.Text = BEGDC.CC.BL.Workflow.Action.ReportToCheck;
                        this.SetLblRunFlowActorsName(description, stepCode, appTypeID);
                        cellArray.Add(this.GetStampCell(BEGDC.CC.BL.Workflow.Action.ReportToCheck, DateTime.Now.ToString("MM/dd/yyyy"), Pending, "PendingY.gif", Color.Yellow));
                        cellArray.Add(this.GetStampCell(stepCode, "xxxx-xx-xx", Pending, "PendingG.gif", Color.Gray));
                        continue;
                    }

                    if (!(stepCode == End || stepCode == ReRequest))
                    {
                        if (i == 0)
                        {
                            this.lblRunFlowRoleNameStr.Visible = true;
                            this.lblRunFlowRoleName.Text = description.ToString();
                            this.SetLblRunFlowActorsName(description, stepCode, appTypeID);

                            //// CR DeptDirectorApproval
                            //if (appTypeID == CR && stepCode == CRStepDeptDirectorApproval)
                            //{
                            //    cellArray.Add(this.GetStampCell(stepCode, DateTime.Now.ToString("MM/dd/yyyy"), Pending, "PendingY.gif", Color.Yellow));
                            //    cellArray.Add(this.GetStampCell(CRStepCloseApp, "xxxx-xx-xx", Pending, "PendingG.gif", Color.Gray));
                            //    break;
                            //}
                            //else
                            //{
                                  //cellArray.Add(this.GetStampCell(stepCode, DateTime.Now.ToString("MM/dd/yyyy"), Pending, "PendingY.gif", Color.Yellow));
                            //}
                            cellArray.Add(this.GetStampCell(stepCode, DateTime.Now.ToString("MM/dd/yyyy"), Pending, "PendingY.gif", Color.Yellow));
                        }
                        else
                        {
                            //if (appTypeID == CR && stepCode == CRStepCloseApp)
                            //{
                            //    cellArray.Add(this.GetStampCell(CRStepDeptDirectorApproval, "xxxx-xx-xx", Pending, "PendingG.gif", Color.Gray));
                            //    cellArray.Add(this.GetStampCell(CRStepCloseApp, "xxxx-xx-xx", Pending, "PendingG.gif", Color.Gray));
                            //    break;
                            //}
                            //else
                            //{
                            //    cellArray.Add(this.GetStampCell(stepCode, "xxxx-xx-xx", Pending, "PendingG.gif", Color.Gray));
                            //}
                            cellArray.Add(this.GetStampCell(stepCode, "xxxx-xx-xx", Pending, "PendingG.gif", Color.Gray));
                        }
                    }

                }
            }
            return cellArray;
        }

        private BEGDC.CC.Web.Common.StampCell GetStampCell(string actorStr, string dateTimeStr, string actionStr, string imageStr, Color color)
        {
            BEGDC.CC.Web.Common.StampCell stampCell;
            stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");
            stampCell.SetStampInfo(actorStr, dateTimeStr, actionStr, imageStr, color);
            return stampCell;
        }

        #endregion

        #region 显示已操作过的步骤

        private void SetDataSource(DataTable table)
        {
            if (table != null && table.Rows.Count > 0)
            {
                DataRow[] rows = table.Select();

                BEGDC.CC.Web.Common.StampCell stampCell;

                ArrayList cellArray = new ArrayList();
                int targetStep = 0;
                bool isShowNextSteps = true;
                bool isOnlyShowNextOneStep = false;

                for (int i = 0; i < rows.Length; i++)
                {
                    #region Submit
                    //Submit
                    if (rows[i]["ActionName"].ToString() == Submit)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");
                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif", Color.Green);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    #region ReportToApprove

                    if (rows[i]["ActionName"].ToString() == BEGDC.CC.BL.Workflow.Action.ReportToCheck)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");
                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif", Color.Green);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    #region Assign

                    if (rows[i]["ActionName"].ToString() == Assign)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");
                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif", Color.Green);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    #region Check

                    if (rows[i]["ActionName"].ToString() == Check)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");

                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif", Color.Green);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    #region CheckMM

                    if (rows[i]["ActionName"].ToString() == CheckMM)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");

                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif", Color.Green);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    #region Approve

                    if (rows[i]["ActionName"].ToString() == Approve)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");

                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif", Color.Green);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    #region Close

                    if (rows[i]["ActionName"].ToString() == Close)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");

                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif", Color.Green);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    #region Reject

                    if (rows[i]["ActionName"].ToString() == Reject)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");

                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Rejected.gif", Color.Red);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }
                    #endregion

                    #region Ask For More

                    //if (rows[i]["ActionName"].ToString() == AskForMore)
                    //{
                    //    stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");

                    //    stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"), rows[i]["ActionName"].ToString(), "Reviewed.gif");

                    //    if (i + 1 < rows.Length)
                    //    {
                    //        if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                    //        {
                    //            cellArray.Add(stampCell);
                    //        }
                    //    }
                    //    else
                    //    {
                    //        cellArray.Add(stampCell);
                    //    }
                    //}

                    #endregion

                    #region Call Back

                    if (rows[i]["ActionName"].ToString() == BEGDC.CC.BL.Workflow.Action.Callback)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");

                        stampCell.SetStampInfo(rows[i]["Actor"].ToString(), Convert.ToDateTime(rows[i]["ActionDate"]).ToString("MM/dd/yyyy"),
                            BEGDC.CC.BL.Workflow.Action.Drawback, "Rejected.gif", Color.Red);

                        if (i + 1 < rows.Length)
                        {
                            if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                            {
                                cellArray.Add(stampCell);
                            }
                        }
                        else
                        {
                            cellArray.Add(stampCell);
                        }
                    }

                    #endregion

                    //set targetStep value
                    if (i + 1 < rows.Length)
                    {
                        if (rows[i + 1]["ActionName"].ToString() != "Drawback")
                        {
                            targetStep = (int)table.Rows[i]["TargetStep"];
                        }
                    }
                    else
                    {
                        targetStep = (int)table.Rows[i]["TargetStep"];
                    }
                }

                if (rows.Length > 1)
                {
                    // unshow next steps
                    if (rows[rows.Length - 1]["ActionName"].ToString() == BEGDC.CC.BL.Workflow.Action.Callback ||
                        rows[rows.Length - 1]["ActionName"].ToString() == BEGDC.CC.BL.Workflow.Action.Reject)
                    {
                        isShowNextSteps = false;
                    }
                    //only show one next step
                    if (rows[rows.Length - 1]["ActionName"].ToString() == BEGDC.CC.BL.Workflow.Action.Query)
                    {
                        stampCell = (BEGDC.CC.Web.Common.StampCell)this.LoadControl("stampCell.ascx");
                        stampCell.SetStampInfo(rows[rows.Length - 1]["Actor"].ToString(), Convert.ToDateTime(rows[rows.Length - 1]["ActionDate"]).ToString("MM/dd/yyyy"),
                            AskForMore, "Rejected.gif", Color.Red);
                        cellArray.Add(stampCell);
                        isOnlyShowNextOneStep = true;
                    }
                }
                if (isShowNextSteps) cellArray = this.StampCellList(cellArray, targetStep, isOnlyShowNextOneStep);
                this.ControlsAdd(cellArray);
            }
            else
            {
                ArrayList cellArray = new ArrayList();
                cellArray = this.StampCellList(cellArray, 0, false);
                this.ControlsAdd(cellArray);
            }
        }

        #endregion

        #region 结点个数
        /// <summary>
        /// 结点每行显示7个
        /// </summary>
        /// <param name="cellArray"></param>
        private void ControlsAdd(ArrayList cellArray)
        {
            if (cellArray.Count > 0)
            {
                HtmlTable htmlTable = new HtmlTable();
                htmlTable.Border = 0;
                htmlTable.CellPadding = 0;
                htmlTable.CellSpacing = 0;

                int k;
                if (cellArray.Count % 7 == 0)
                {
                    k = cellArray.Count / 7;
                }
                else
                {
                    k = ((int)cellArray.Count / 7) + 1;
                }

                ArrayList rowArray = new ArrayList();
                for (int m = 0; m < k; m++)
                {
                    HtmlTableRow htmlRow = new HtmlTableRow();
                    rowArray.Add(htmlRow);
                }

                IEnumerator rowEnum = rowArray.GetEnumerator();
                rowEnum.MoveNext();

                for (int j = 0; j < cellArray.Count; j++)
                {
                    HtmlTableCell htmlCell = new HtmlTableCell();

                    htmlCell.Controls.Add((BEGDC.CC.Web.Common.StampCell)cellArray[j]);
                    ((HtmlTableRow)rowEnum.Current).Cells.Add(htmlCell);

                    if (((j + 1) % 7 == 0) || j == cellArray.Count - 1)
                    {
                        htmlTable.Rows.Add(((HtmlTableRow)rowEnum.Current));
                        rowEnum.MoveNext();
                    }
                }

                this.Controls.Add(htmlTable);
            }
        }

        #endregion

        #region 已执行过的Stamp

        private void GetStampByAppID(int AppID)
        {
            IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            DataTable table = queryService.QueryActionLog(AppID);
            this.SetDataSource(table);
        }

        #endregion

        #region 设置当前操作人

        private void SetLblRunFlowActorsName(string description, string stepCode, int appTypeID)
        {
            this.lblRunFlowActorsNameStr.Visible = true;
            if (this.Request.QueryString["AppID"] != null)
            {
                int appid = Convert.ToInt32(this.Request.QueryString["AppID"]);
                this.lblRunFlowActorsName.Text = this.GetActorsOfCurrentStep(stepCode, appTypeID, appid);
            }
            else
            {
                this.lblRunFlowActorsName.Text = description;
            }
        }

        private string GetActorsOfCurrentStep(string stepName, int appType, int appID)
        {
            string actors = string.Empty;

            IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            //Get actor info by step
            DataTable table = queryService.GetRoleAndEmpForStep(stepName, appType, appID);

            if (table != null && table.Rows.Count > 0)
            {
                DataRow[] rows = table.Select();
                IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;

                if (!string.IsNullOrEmpty(rows[0]["EmployeeNo"].ToString()))
                {
                    Employee employee = userService.GetUserByID(rows[0]["EmployeeNo"].ToString());
                    actors = employee.NameEN;

                    if (stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCR_AssignApp || stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCV_Assignment)
                    {
                        if (this.lblRunFlowRoleName.Visible && lblRunFlowRoleName.Text.Equals("AssignApp"))
                        {
                            actors = ReturnActorList(userService, (int)rows[0]["RoleID"]);
                        }
                    }
                }
                else
                {
                    //IList list = userService.GetUserByRole((int)rows[0]["RoleID"]);
                    //for (int i = 0; i < list.Count; i++)
                    //{
                    //    BEGDC.CC.Entity.UserRole userRole = (BEGDC.CC.Entity.UserRole)list[i];
                    //    Employee employee = userRole.Employee;
                    //    actors += employee.NameEN + ",";
                    //}
                    //actors = actors.Substring(0, actors.Length - 1);
                    actors = ReturnActorList(userService, (int)rows[0]["RoleID"]);
                }
            }
            return actors;
        }

        private string ReturnActorList(IUser userService, int roleID)
        {
            string actors = string.Empty;
            IList list = userService.GetUserByRole(roleID);
            for (int i = 0; i < list.Count; i++)
            {
                BEGDC.CC.Entity.UserRole userRole = (BEGDC.CC.Entity.UserRole)list[i];
                Employee employee = userRole.Employee;
                actors += employee.NameEN + ",";
            }
            return (actors.IndexOf(",") > 0) ? actors.Substring(0, actors.Length - 1) : actors;
        }

        #endregion

        /// <summary>
        /// 恢复当前Assgin的Mgr
        /// </summary>
        /// <param name="stepName"></param>
        /// <returns></returns>
        //private bool ResumeOCAssignPersons(string stepName, Employee employee)
        //{
        //    //首先，AppType是海外的
        //    string PDType = this.Request.QueryString["PDType"];
        //    if (!(PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCR.ToString() || PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCV.ToString())) return false;

        //    //其次，必须是AssignApp
        //    if (!(stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCR_AssignApp || stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCV_Assignment)) return false;

        //    //最后，数据库保存的执行者是否拥有Mgr角色
        //    IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
        //    if (!userService.IsInRole(employee.NameEN, BEGDC.CC.BL.Workflow.UserRole.OCRMgr) &&
        //        PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCR.ToString() ||
        //        stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCR_AssignApp)
        //        return true;
        //    if (!userService.IsInRole(employee.NameEN, BEGDC.CC.BL.Workflow.UserRole.OCVMgr) &&
        //       PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCV.ToString())
        //        return true;

        //    return false;
        //}

        #region 是否添加ReportTo图章

        private bool IsReportToApprove(string stepName, int appType, ArrayList cellArray)
        {
            //if (stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCR_Request ||
            //    stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCV_Apply)
            //    return true;

            if (!(stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCR_AssignApp ||
                stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCV_Assignment))
                return false;

            int appID = Convert.ToInt32(this.Request.QueryString["AppID"]);
            if (appID <= 0) return false;

            string PDType = this.Request.QueryString["PDType"];
            if (!(PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCR.ToString() || PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCV.ToString())) return false;

            StampCell cell = cellArray[cellArray.Count - 1] as StampCell;
            if (cell.stampActionStr.Equals(BEGDC.CC.BL.Workflow.Action.ReportToCheck)) return false;

            BEGDC.CC.BL.Workflow.CCIdentity identity = (BEGDC.CC.BL.Workflow.CCIdentity)this.Page.User.Identity;
            IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;

            //Get Applicant
            IQuery query = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            BEGDC.CC.Entity.Application app = query.GetApplication(appID);
            if (app == null || app.RequesterNo.ReportTo == null) return false;

            //Get actor info by step
            Employee employee = null;
            DataTable table = query.GetRoleAndEmpForStep(stepName, appType, appID);
            if (table != null && table.Rows.Count > 0)
            {
                DataRow[] rows = table.Select();
                if (!string.IsNullOrEmpty(rows[0]["EmployeeNo"].ToString()))
                    employee = userService.GetUserByID(rows[0]["EmployeeNo"].ToString());
            }
            if (employee == null) return false;

            //1、Flow type is OCR
            //2、not OCRMgr
            //3、StepName is AssignApp
            //4、Current user is Requester's ReportTo
            if ((PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCR.ToString()) &&
                !(userService.IsInRole(employee.NameEN, BEGDC.CC.BL.Workflow.UserRole.OCRMgr)) &&
                string.Equals(app.RequesterNo.ReportTo.Id, employee.Id) &&
                (stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCR_AssignApp))
                return true;

            //1、Flow type is OCV
            //2、not OCRMgr
            //3、StepName is AssignApp
            //4、Current user is Requester's ReportTo
            if ((PDType == BEGDC.CC.BL.Workflow.FlowTypes.OCV.ToString()) &&
               !(userService.IsInRole(employee.NameEN, BEGDC.CC.BL.Workflow.UserRole.OCVMgr)) &&
               string.Equals(app.RequesterNo.ReportTo.Id, employee.Id) &&
               (stepName == BEGDC.CC.BL.Workflow.WorkTransition.OCV_Assignment))
                return true;

            return false;
        }

        #endregion
    }
}
